package kr.co.kiso.usr.common.controller;

import java.io.File;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.co.kiso.usr.common.BaseController;
import kr.co.kiso.usr.common.service.CommonService;
import kr.co.kiso.usr.common.vo.CommonVo;
import kr.co.kiso.usr.utils.PaginationInfo;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/cm/ma/*")
public class CommonController extends BaseController {
	@Autowired
	private CommonService commonService;
	
	Logger log = Logger.getLogger(getClass());

	/**
	 * 상위 코드 목록 리스트
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView CMNG010Q(CommonVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		isMember(req,res);
		ModelAndView mav = new ModelAndView();

		// 페이징 시작
		PaginationInfo paginationInfo = new PaginationInfo();
		int currnetPageNo = vo.getCurrentPageNo() <= 1 ? 1 : (Integer) vo
				.getCurrentPageNo();
		vo.setCurrentPageNo(currnetPageNo);
		vo.setParamMap(requestPram2Map(req));
//		if (vo.getSch_title() != null && !vo.getSch_title().equals("")) {
//			vo.setSch_title("%" + vo.getSch_title() + "%");
//		}
		int iTotalCnt = commonService.CMNG010Q_CNT();
		vo.setTotalRecordCount(iTotalCnt);
		
		if(iTotalCnt>0){
			// 상위 코드목록
			mav.addObject("list", commonService.CMNG010Q(vo));
			
			mav.addObject("pageHtml", vo.getToHtml("./CMNG010Q.do"));
			mav.addObject("paginationInfo", vo);
		}
		mav.addObject("phoneList", commonService.CMNG060Q("00100"));
		
		

		return mav;
	}
	
	/**
	 * 상세 목록 출력
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView CMNG020Q(CommonVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		isMember(req,res);
		ModelAndView mav = new ModelAndView();

		// 페이징 시작
		PaginationInfo paginationInfo = new PaginationInfo();
		int currnetPageNo = vo.getCurrentPageNo() <= 1 ? 1 : (Integer) vo
				.getCurrentPageNo();
		vo.setCurrentPageNo(currnetPageNo);
		vo.setParamMap(requestPram2Map(req));

		int iTotalCnt = commonService.CMNG020Q_CNT(vo);
		vo.setTotalRecordCount(iTotalCnt);
		
		if(iTotalCnt>0){
			// 상위 코드목록
			mav.addObject("list", commonService.CMNG020Q(vo));
			
			mav.addObject("pageHtml", vo.getToHtml("./CMNG020Q.do"));
			mav.addObject("paginationInfo", vo);
		}

		return mav;
	}
	
	
	/**
	 * 등록페이지
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView CMNG030Q(CommonVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		isMember(req,res);
		ModelAndView mav = new ModelAndView();
		mav.addObject("code_p_seq_no",vo.getCode_p_seq_no());
		return mav;
	}
	
	/**
	 * 코드 등록 프로세스
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView CMNG030T(CommonVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		isMember(req,res);
		ModelAndView mav = new ModelAndView("redirect:./CMNG020Q.do?code_p_seq_no="+vo.getCode_p_seq_no());
		commonService.CMNG030T(vo);
		return mav;
	}

	/**
	 * 코드 수정 프로세스
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView CMNG040T(CommonVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		isMember(req,res);
		ModelAndView mav = new ModelAndView("redirect:./CMNG020Q.do?code_p_seq_no="+vo.getCode_p_seq_no());
		commonService.CMNG040T(vo);
		return mav;
	}
	
	/**
	 * 코드 삭제 프로세스
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView CMNG050T(CommonVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		isMember(req,res);
		ModelAndView mav = new ModelAndView("redirect:./CMNG020Q.do?code_p_seq_no="+vo.getCode_p_seq_no());
		commonService.CMNG050T(vo);
		return mav;
	}
	
	/**
	 * 파일 다운로드
	 * @param model
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping
	public ModelAndView downloadFile(ModelMap model, HttpServletRequest req)
			throws Exception {

		String key = "file.encoding"; 
		
		String filePath="";
		//유닉스 서버 인코딩
		if("8859_1".equals(System.getProperty(key))){
			filePath = new String(req.getSession().getServletContext().getRealPath(req.getParameter("m_file_nm")).getBytes("UTF-8"), "8859_1");
		}else{
			filePath = req.getSession().getServletContext().getRealPath(req.getParameter("m_file_nm"));
		}
		
		File downLoadFile = new File(filePath);
//		log.info("path  : " + req.getSession().getServletContext().getRealPath(req.getParameter("m_file_nm")));
//		log.info("file is file" + downLoadFile.isFile());
//		log.info("file is file22222222222222222222" + downLoadFile222.isFile());
		
		HashMap fileInfo = new HashMap();
		fileInfo.put("downloadFile", downLoadFile);
		fileInfo.put("fileName", req.getParameter("v_file_nm"));
		

		return new ModelAndView("download", "fileMap", fileInfo);
	}
}
